package cn.com.bob.alipay.web.controller;

import cn.com.bob.alipay.web.utils.AlipayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;


/**
 * @Author Bob
 * @Version：1.0
 */
@Controller
public class AlipayController {

    @Autowired
    private AlipayUtils  alipayUtils;

    @GetMapping("/")
    public String index(){
        return "index";
    }

    @PostMapping("/create")
    public String create(String id, String price, String title, Model model){
        String pay = alipayUtils.pay(id, price, title);
        model.addAttribute("form",pay);
        return "pay";
    }

    @GetMapping("/return")
    public String returnNotice(String out_trade_no,Model model){
        System.out.println(out_trade_no);
        String query = alipayUtils.query(out_trade_no);
        System.out.println(query);
        model.addAttribute("query",query);
        return "query";
    }

    @PostMapping("/notify")
    public void notifyUrl(String trade_no,String total_amount,String trade_status){
        System.out.println("支付宝订单编号：" + trade_no + ",订单金额：" + total_amount + ",订单状态：" + trade_status);
    }

    @GetMapping("/refund")
    public String refundPage(){
        return "refund";
    }
    @PostMapping("/refundNotice")
    public String refund(String trade_no,String price,String id,Model model){
        String refund = alipayUtils.refund(trade_no, price, id);
        model.addAttribute("refund",refund);
        return "refundPage";
    }

    @GetMapping("/close/{id}")
    public String close(@PathVariable("id") String id){
        alipayUtils.closeOrder(id);
        return "close";
    }
}
